package com.futu.common.exceptions;




import com.futu.common.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MultipartException;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    // private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 系统异常处理，比如：404,500
     *
     * @param req
     * @param e
     * @return
     * @throws Exception
     */
//    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResultVo defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
        // log.error("", e);
        log.error(e.getMessage());
        //判断是否属于自定义异常


        if (e instanceof org.springframework.web.servlet.NoHandlerFoundException) {
        	return ResultVo.error(404,"不存在页面请求");
       }

        if (e instanceof ApiException) {

            ApiException customException = (ApiException) e;

            return ResultVo.error(customException.getCode(), customException.getMessage());
        } else if (e instanceof MultipartException){
            log.error("系统异常{}", e);
            return ResultVo.error(1000, "上传文件异常");
        } else if(e instanceof MethodArgumentNotValidException) {
            MethodArgumentNotValidException methodArgumentNotValidException=(MethodArgumentNotValidException)e;
            return ResultVo.error(1002,methodArgumentNotValidException.getBindingResult().getFieldError().getDefaultMessage());
        }else{
            log.error("系统异常{}", e);
            return ResultVo.error(1001, "系统参数异常");
        }
    }



//    @ExceptionHandler(value = InsufficientAuthenticationException.class)
//    @ResponseBody
//    public ResultVo jsonErrorHandle(HttpServletRequest req, InsufficientAuthenticationException e) {
//        return ResultVo.error(0, e.getMessage());
//    }

}
